package top.csy8888.ch08.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;

/**
 * @author CSY
 * @date 2025 - 10 - 16
 */
@Configuration
public class RabbitConfig {
    //普通的交换机、队列、路由key
    public static final String EXCHANGE_NAME = "test.exchange";
    public static final String ROUTING_KEY = "test.key";
    public static final String QUEUE_NAME = "test.queue";

    //死信交换机、队列、路由key
    public static final String DEAD_EXCHANGE_NAME = "dead.exchange";
    public static final String DEAD_ROUTING_KEY = "dead.key";
    public static final String DEAD_QUEUE_NAME = "dead.queue";

    /**
     * 声明普通队列，并且当重试失败后将消息自动投递到死信交换机
     * @return
     */
    @Bean
    public Queue queue() {
        //nonDurable方法创建不持久化的交换机，如果需要持久化则调用durable方法
        return QueueBuilder.nonDurable(QUEUE_NAME)
                //是否需要自动删除
                //.autoDelete()
                //通过参数设置和死信交换机进行关联
                .withArgument("x-dead-letter-exchange", DEAD_EXCHANGE_NAME)
                //设置四星队列路由key
                .withArgument("x-dead-letter-routing-key", DEAD_ROUTING_KEY)
                .build();
    }

    @Bean
    public Exchange exchange() {
        return new DirectExchange(EXCHANGE_NAME,false,true);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder
                .bind(queue())
                .to(exchange())
                .with(ROUTING_KEY)
                .noargs();
    }

    /**
     * 声明死信交换机（DLX），死信交换机也可以是Direct或者其他类型
     */
    @Bean
    public Exchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE_NAME,false,true);
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue deadQueue() {
        return new Queue(DEAD_QUEUE_NAME,false);
    }

    @Bean
    public Binding bindingDead() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(DEAD_ROUTING_KEY).noargs();
    }

}
